function onLoad(){
	var user=getCookie("username");
	if(user==""){
		window.location="/login";
		return;
	}
	initNav();
	var edit=document.getElementById("infoEdit");
	edit.style.display="none";
}

function logout(){
	if(confirm("请确认注销操作")){
		document.cookie="username=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
		document.cookie="userid=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
		window.location="/";
	}else{
		return;
	}
}

function modifyDeal(gid,buyer,seller,operation,detail){
/*
operation允许的值：
'remove'	删除交易
'status'	改变交易状态
'price'		改变价格
*/
	event.stopPropagation();
	$.ajax({
		type: 'POST',
		url: '/modify_deal',
		data: {
			'gid': gid,
			'buyer': buyer,
			'seller': seller,
			'do': operation,
			'text': detail
		},
		success: function(){
			alert("更新:"+detail);
			window.location="/myinfo";
		},
		fail: function(){
			alert("请求失败");
			window.location="/myinfo";
		},
		error: function(){
			alert("服务器内部错误");
			window.location="/myinfo";
		}
	});
}

// function initDealList(){
// 	items=document.getElementsByClassName("deal");
	
// }

//删除商品
function removeGoods(gid){
	event.stopPropagation();
	$.ajax({
		type: 'post',
		url: '/remove_goods',
		data: {
			'gid': gid
		},
		success: function(arg){
			alert(arg);
			window.location="/myinfo";
		},
		fail: function(){
			alert("请求失败");
			window.location="/myinfo";
		},
		error: function(){
			alert("服务器内部错误");
			window.location="/myinfo";
		}
	});
}

//删除收藏
function removeFavorites(uid,gid){
	event.stopPropagation();
	$.ajax({
		type: 'post',
		url: '/remove_favorites',
		data: {
			'gid': gid,
			'uid': uid
		},
		success: function(arg){
			alert(arg);
			window.location="/myinfo";
		},
		fail: function(){
			alert("请求失败");
			window.location="/myinfo";
		},
		error: function(){
			alert("服务器内部错误");
			window.location="/myinfo";
		}
	});
}

//点击修改个人信息按钮后
function editInfo(){
	var display=document.getElementById("infoDisplay");
	display.style.display="none";
	var display=document.getElementById("infoEdit");
	display.style.display="block";

	var blocks=document.getElementsByClassName("infoShow");
	var inputs=document.getElementsByClassName("infoText");
	for(var i=0;i<7;i++){
		inputs[i].value=blocks[i].innerHTML;
	}
}

//修改个人信息时点击确认按钮后
function confirmInfo(uid){
	var inputs=document.getElementsByClassName("infoText");
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].value==""){
			alert("输入的信息不完整");
			return;
		}
	}
	if(confirm("确定修改信息吗")){
		//重要：ajax请求中json左值必须是user表中的列名
		$.ajax({
			type: 'post',
			url: '/modify_info',
			data: {
				'uID': uid,
				'uName': inputs[0].value,
				'uCampus': inputs[1].value,
				'uTel': inputs[2].value,
				'uVx': inputs[3].value,
				'uQQ': inputs[4].value,
				'uEmail': inputs[5].value,
				'schoolID': inputs[6].value
			},
			success: function(arg){
				window.location.reload();
				alert(arg);
			},
			fail: function(){
				alert("请求失败");
			},
			error: function(){
				alert("服务器内部错误")
			}
		});
		// var display=document.getElementById("infoDisplay");
		// display.style.display="block";
		// var display=document.getElementById("infoEdit");
		// display.style.display="none";
	}
}

//修改个人信息时点击取消按钮后
function cancelEdit(){
	var display=document.getElementById("infoDisplay");
	display.style.display="block";
	var display=document.getElementById("infoEdit");
	display.style.display="none";
}

//修改商品信息
function modifyGoods(gid){
	window.location="/upload_update_"+String(gid);
}